package com.yogee.topseller.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.PayActivity;
import com.yogee.topseller.bean.HuiyuanModel;
import com.yogee.topseller.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class HuiyuanAdapter extends RecyclerView.Adapter<HuiyuanAdapter.ItemHolder> {

    private Context context;
    private List<HuiyuanModel> beans = new ArrayList<>();

    private int mSelectedPos = -1;
    private PayActivity.Listener listener;

    public HuiyuanAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<HuiyuanModel> beans) {
        this.beans = beans;

        notifyDataSetChanged();
    }
    public void setSelect(List<HuiyuanModel> beans,int pos){
        this.beans = beans;
        this.mSelectedPos = pos;
        notifyDataSetChanged();
    }

    public void setListener(PayActivity.Listener listener){
        this.listener = listener;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_huiyuan, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {

        if(mSelectedPos==position){
            holder.rl_select1.setVisibility(View.VISIBLE);
        }else{
            holder.rl_select1.setVisibility(View.GONE);
        }

        holder.tv_huiyuan_money.setText(beans.get(position).getPrice());
        holder.tv_huiyuan_time.setText(beans.get(position).getDescription());
        holder.tv_huiyuan_yuanjia.setText("原价  "+beans.get(position).getOriginalPrice());
        holder.tv_huiyuan_yuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
        if(position==0){
            holder.tv_ms.setText("原价: "+beans.get(position).getOriginalPrice()+"元");
            holder.tv_ms.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
            holder.tv_ms.setVisibility(View.VISIBLE);
//            holder.tv_xian.setVisibility(View.VISIBLE);
    }else{
        holder.tv_ms.setVisibility(View.GONE);
//            holder.tv_xian.setVisibility(View.GONE);
    }
//        if(beans.get(position).getPaywords()!=null&&!"".equals(beans.get(position).getPaywords())){
//            holder.tv_ms.setText(beans.get(position).getPaywords());
//            holder.tv_ms.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
//            holder.tv_ms.setVisibility(View.VISIBLE);
//        }else{
//            holder.tv_ms.setVisibility(View.GONE);
//        }
        LogUtils.e("sfz","icon:"+beans.get(position).getImg());
        Glide.with(context).load(beans.get(position).getImg()).into(holder.img_huiyuan);

        holder.rl_background.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int old_pos = mSelectedPos;

                //设置新Item的勾选状态
                mSelectedPos = position;
                if(old_pos>=0){
                    notifyItemChanged(old_pos);
                }
                notifyItemChanged(mSelectedPos);
                listener.getSelect(beans.get(position));

            }
        });


    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_huiyuan_time)
        TextView tv_huiyuan_time;
        @BindView(R.id.tv_huiyuan_money)
        TextView tv_huiyuan_money;
        @BindView(R.id.tv_huiyuan_yuanjia)
        TextView tv_huiyuan_yuanjia;
        @BindView(R.id.rl_background)
        RelativeLayout rl_background;
        @BindView(R.id.img_huiyuan)
        ImageView img_huiyuan;
        @BindView(R.id.tv_ms)
        TextView tv_ms;
        @BindView(R.id.rl_select1)
        RelativeLayout rl_select1;
        @BindView(R.id.tv_xian)
        TextView tv_xian;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}